<!-- 功能：外接UPS - 外接UPS设置 弹窗 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月24日 14:23:00 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="外接UPS设置" width="600px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-form :label-col="{ span:6 }" :wrapper-col="{span: 14}">
      <a-form-item label="所属控制器" v-bind="validateInfos.controller">
        <a-select v-model:value="modelRef.controller" placeholder="请选择所属控制器">
          <a-select-option value="controller-1">controller-1</a-select-option>
          <a-select-option value="controller-2">controller-2</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="型号" v-bind="validateInfos.xingHao">
        <a-select v-model:value="modelRef.xingHao" placeholder="请选择型号">
          <a-select-option value="C1K">C1K</a-select-option>
          <a-select-option value="C2K">C2K</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="UPS剩余电量关机" v-bind="validateInfos.dianLiang">
        <a-input v-model:value="modelRef.dianLiang" placeholder="UPS剩余电量,1到100的整数"></a-input>
      </a-form-item>
      <a-form-item label="UPS关机延迟时间" v-bind="validateInfos.guanJiShiJian">
        <a-input v-model:value="modelRef.guanJiShiJian" placeholder="UPS关机延迟时间,1到120的整数"></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  controller: '',
  xingHao: 'C1K',
  dianLiang: '',
  guanJiShiJian: ''
})

const rulesRef = reactive({
  controller: [
    {
      required: true,
      message: '请选择所属控制器'
    }
  ],
  dianLiang: [
    {
      required: true,
      message: '请输入UPS剩余电量关机'
    }
  ],
  guanJiShiJian: [
    {
      required: true,
      message: '请输入UPS关机延迟时间'
    }
  ]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log('外接UPS设置>>', toRaw(modelRef))
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}
</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
</style>